package com.tnsw.facebookautoliker.helper.command;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import com.tnsw.coreutils.MyLog;

import android.text.format.DateFormat;

public class FBObjectParser {
	
	private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
	
	public static int STATUSTYPE_UPLOADED_PHOTO = 1;		// status_type: added_photo
	public static int STATUSTYPE_SHARED_STORY = 2;			// status_type: shared_story
	public static int STATUSTYPE_UPLOADED_VIDEO = 3;		// status_type: added_video
	public static int STATUSTYPE_MOBILE_STATUS_UPDATE = 4;	// status_type: mobile_status_update
	public static int STATUSTYPE_TAGGED_IN_PHOTO = 5; 		// status_type: tagged_in_photo

	private String tag = "FBObjectParser";
	private String jsonStr;
	private JSONObject jo;
	
	private String id = "";
	private String updatedTimeStr = "";
	private String createdTimeStr = "";
	private String objectId = "";
	private String statusType = "";
	private String fromId = "";
	private String fromName = "";
	private String postId = "";
	
	public FBObjectParser(String jsonStr) {
		this.jsonStr = jsonStr;
	}
	
	public FBObjectParser(JSONObject jo) {
		this.jo = jo;
		this.jsonStr = jo.toString();
	}
	
	public JSONObject getJSONObject() {
		return(this.jo);
	}
	
	public String getRawJsonString() {
		return(this.jsonStr);
	}
	
	public String getUpdateTimeStr() {
		return(this.updatedTimeStr);
	}
	
	public String getFromId() {
		return(this.fromId);
	}
	
	public String getFromName() {
		return(this.fromName);
	}
	
	public String getPostId() {
		return(this.postId);
	}
	
	public long getCreatedTimeMil() {
		try {
			Date d = df.parse(this.createdTimeStr);
			return(d.getTime());
		} catch (Exception e) {
			MyLog.e(tag, "Cannot parse date: " + this.createdTimeStr);
		}
		
		
		return(-1);
	}
	
	public long getUpdatedTimeMil() {
		
		try {
			Date d = df.parse(this.updatedTimeStr);
			return(d.getTime());
		} catch (Exception e) {
			MyLog.e(tag, "Cannot parse date: " + this.updatedTimeStr);
		}
		
		
		return(-1);
	}
	
	public String getCreatedTimeStr() {
		return(this.createdTimeStr);
	}
	
	public String getObjectId() {
		return(this.objectId);
	}
	
	public String getId() {
		return(this.id);
	}
	
	public String getStatusType() {
		return(this.statusType);
	}
	
	public void parse() throws NotSupportedFBObjectException, JSONException {
		if (this.jo == null) {
			this.jo = new JSONObject(this.jsonStr);	
		}
		
		if (jo.has("id")) {
			this.id = jo.getString("id");
		}
		
		if (jo.has("updated_time")) {
			this.updatedTimeStr = jo.getString("updated_time");
		}
		
		if (jo.has("created_time")) {
			this.createdTimeStr = jo.getString("created_time");
		}
		
		if (jo.has("object_id")) {
			this.objectId = jo.getString("object_id");
		}
		
		if (jo.has("status_type")) {
			this.statusType = jo.getString("status_type");
		}
		
		if (jo.has("from")) {
			JSONObject jFrom = jo.getJSONObject("from");
			this.fromId = jFrom.getString("id");
			this.fromName = jFrom.getString("name");
		}
		
		if (jo.has("post_id")) {
			this.postId = jo.getString("post_id");
		}
		
	}
	
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		
		sb.append("id=" + this.id);
		sb.append(", object_id=" + this.objectId);
		sb.append(", created_time=" + this.createdTimeStr + "(" + this.getCreatedTimeMil() + ")");
		sb.append(", updated_time=" + this.updatedTimeStr + "(" + this.getUpdatedTimeMil() + ")");
		sb.append(", from_id=" + this.fromId);
		sb.append(", from_name=" + this.fromName);
		sb.append(", post_id=" + this.postId);
		
		return(sb.toString());
	}
	
	
}
